<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增商铺类型')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-WxShopType-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">序号：</label>
                <div class="col-sm-8">
                    <input name="sortNum" id="sortNum" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">类别名称：</label>
                <div class="col-sm-8">
                    <input name="typeName" id="typeName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">上传图片：</label>
                <div class="col-sm-8">
                    <div class="file-loading">
                        <input id="imgFile" name="imgFile" class="file" type="file"  data-theme="fas" accept=".jpg,.img,.png">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script type="text/javascript">
        var prefix = ctx + "WxShopType/WxShopType"
        $("#form-WxShopType-add").validate({
            onkeyup: false,
            rules:{
                typeName:{
                    remote: {
                        url: prefix + "/checkNameUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "typeName": function () {
                                return $.common.trim($("#typeName").val());
                            }
                        },
                        dataFilter: function (data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },
                sortNum:{
                    remote: {
                        url: prefix + "/checkSortNumUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "sortNum": function () {
                                return $.common.trim($("#sortNum").val());
                            }
                        },
                        dataFilter: function (data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },
            },
            messages: {
                "typeName":{
                    remote: "类名已存在"
                },
                "sortNum":{
                    remote: "序号重复"
                }
            },
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                if ($('#imgFile')[0].files[0] == null) {
                    $.modal.alertWarning("请先选择文件路径");
                    return false;
                }
                var formData = new FormData($("#form-WxShopType-add")[0]);
                formData.append('imgFile', $('#imgFile')[0].files[0]);
                var loading;
                $.ajax({
                    url: prefix + "/add",
                    type: 'post',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    beforeSend:function(){
                        loading = layer.load(1, {
                            shade: [0.5,'#000'] //0.1透明度的背景
                        })

                    },
                    success: function(result) {
                        layer.close(loading);
                        $.operate.successCallback(result);
                    }
                });


            }
        }


        $("#imgFile").fileinput({
            uploadAsync:false,//默认异步上传
            showUpload:false,//是否显示上传按钮
        })
    </script>
</body>
</html>